package com.cpi.cornpp.distributioncost.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributioncost.exception.DistributionCostVersionException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;

public class DistributionCostVersionDAO extends DBDAO {
	private static final String SQL_EXECUTE_SP_CREATE_COST_DISTRIBUTION = "exec sp_Cost_Estimate_Full ?";//$NON-NLS-1$
	private static final String SQL_EXECUTE_SP_REMOVE_COST_DISTRIBUTION = "exec sp_Cost_Estimate_Delete ?";//$NON-NLS-1$
	public void add(int versionId) throws DistributionCostVersionException{
		PreparedStatement pstm = null;		
		StringBuffer sql = null;
		try{
			sql = new StringBuffer(0);
			sql.append(SQL_EXECUTE_SP_CREATE_COST_DISTRIBUTION);
			pstm = prepareSQL(sql.toString(), dsName);
			pstm.setInt(1, versionId);
			executeInsert(pstm);
		} catch(PoolException poolException){
			DistributionCostVersionException distributionCostVersionException = null;
			poolException.printStackTrace(System.out);
			distributionCostVersionException = new DistributionCostVersionException(poolException, CornPPException.LAYER_DAO, CornPPException.ACTION_INSERT);
			throw distributionCostVersionException;
		} catch(SQLException sqlException){
			DistributionCostVersionException distributionCostVersionException = null;
			sqlException.printStackTrace(System.out);
			distributionCostVersionException = new DistributionCostVersionException(sqlException, CornPPException.LAYER_DAO, CornPPException.ACTION_INSERT);
			throw distributionCostVersionException;
		}finally {
			try{
				closeConnection(pstm.getConnection());
			}catch(Exception exception){
			}
		}
	}
	
	public void delete(int versionId) throws DistributionCostVersionException{
		PreparedStatement pstm = null;		
		StringBuffer sql = null;
		try{		
			sql = new StringBuffer(0);
			sql.append(SQL_EXECUTE_SP_REMOVE_COST_DISTRIBUTION);
			pstm = prepareSQL(sql.toString(), dsName);
			pstm.setInt(1, versionId);
			executeInsert(pstm);
		} catch(PoolException poe){
			DistributionCostVersionException distributionCostVersionException = null;
			poe.printStackTrace(System.out);
			distributionCostVersionException = new DistributionCostVersionException(poe, CornPPException.LAYER_DAO, CornPPException.ACTION_DELETE);
			throw distributionCostVersionException;
		} catch(SQLException sqle){
			DistributionCostVersionException distributionCostVersionException = null;
			sqle.printStackTrace(System.out);
			distributionCostVersionException = new DistributionCostVersionException(sqle, CornPPException.LAYER_DAO, CornPPException.ACTION_DELETE);
			throw distributionCostVersionException;
		}finally {
			try{
				closeConnection(pstm.getConnection());
			}catch(Exception exception){
			}
		}
	}
}